package com.item.mobile;

import static com.item.mobile.MobileUtil.checkParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.dialect.PaginationSupport;
import com.base.mobile.MobileInfo;
import com.base.mobile.server.annotation.Mobile;
import com.base.mobile.server.annotation.MobileMethod;
import com.base.util.DateUtil;
import com.base.util.StringUtil;
import com.item.dao.model.EssenceAuthor;
import com.item.dao.model.News;
import com.item.dao.model.NewsExample;
import com.item.dao.model.Note;
import com.item.dao.model.NoteAuthor;
import com.item.dao.model.NoteCollect;
import com.item.dao.model.NoteCollectExample;
import com.item.dao.model.NoteCollectKey;
import com.item.dao.model.NoteComment;
import com.item.dao.model.NoteCommentExample;
import com.item.dao.model.NoteDetail;
import com.item.dao.model.NoteDetailExample;
import com.item.dao.model.NoteExample;
import com.item.dao.model.NoteLock;
import com.item.dao.model.NoteLockKey;
import com.item.dao.model.NotePraise;
import com.item.dao.model.NotePraiseExample;
import com.item.dao.model.NoteUser;
import com.item.dao.model.NoteUserExample;
import com.item.dao.model.Subject;
import com.item.dao.model.SubjectDetail;
import com.item.dao.model.SubjectDetailExample;
import com.item.dao.model.SubjectExample;
import com.item.dao.model.Unread;
import com.item.dao.model.User;
import com.item.daoEx.model.NewsEx;
import com.item.daoEx.model.NoteUserEx;
import com.item.service.EssenceAuthorService;
import com.item.service.NewsService;
import com.item.service.NoteAuthorService;
import com.item.service.NoteCollectService;
import com.item.service.NoteCommentService;
import com.item.service.NoteDetailService;
import com.item.service.NoteLockService;
import com.item.service.NotePraiseService;
import com.item.service.NoteService;
import com.item.service.NoteUserService;
import com.item.service.SubjectDetailService;
import com.item.service.SubjectService;
import com.item.service.UnreadService;
import com.item.service.UserService;
import com.item.util.ApiUtil;
import com.item.util.ConstantResources;
import com.mobile.api.proto.MAppNote.MMyCollect;
import com.mobile.api.proto.MAppNote.MMyCollectList;
import com.mobile.api.proto.MAppNote.MNoteBook;
import com.mobile.api.proto.MAppNote.MNoteBookDetail;
import com.mobile.api.proto.MAppNote.MNoteBookList;
import com.mobile.api.proto.MAppNote.MNoteComment;
import com.mobile.api.proto.MAppNote.MNoteComments;
import com.mobile.api.proto.MAppNote.MNoteDetailList;
import com.mobile.api.proto.MAppNote.MSubject;
import com.mobile.api.proto.MAppNote.MSubjectDetail;
import com.mobile.api.proto.MAppNote.MSubjectList;
import com.mobile.api.proto.MAppQuestion.MQuestion;
import com.mobile.api.proto.MSystem.MRet;

@Component
@Mobile
public class MAppNote {
	@Autowired
	private SubjectService  subService;
	@Autowired
	private SubjectDetailService  subDetailService;
	@Autowired
	private NoteService  noteService;
	@Autowired
	private NoteDetailService noteDetailService;
	@Autowired
	private NotePraiseService notePraiseService;
	@Autowired
	private NoteCommentService noteCommentService;
	@Autowired
	private NoteAuthorService authortService;
	@Autowired
	private NewsService newsService;
	@Autowired
	private UserService userService;
	@Autowired
	private NoteUserService noteUserService;
	@Autowired
	private NoteCollectService noteCollectService;
	@Autowired
	private EssenceAuthorService essenceAuthorService;
	@Autowired
	private NoteLockService noteLockService;
	@Autowired
	private UnreadService readService;

	
	/**
	 * 获取笔记本列表，两个维度：科目和小科目
	 * @param mobileInfo
	 * @param type
	 * @param page
	 * @param limit
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MNoteBookList", isLogin = true, isPage = false) 
	public MNoteBookList.Builder noteBookList(MobileInfo mobileInfo,Integer type,String chapterId,Integer page,Integer limit) throws Exception {
		if(type == null){
			type = 1;
		}
		if(page == null)
			page = 1;
		if(limit == null)
			limit = 50;
		MNoteBookList.Builder ret = MNoteBookList.newBuilder();
//		//拿一条广告
//		NewsExample newsex = new NewsExample();
//		newsex.createCriteria().andTypeEqualTo(2).andIsShowEqualTo(1);
//		PaginationSupport.setContext(1, 1, null);
//		newsex.setOrderByClause("top DESC");
//		List<News> listsNews = newsService.selectByExample(newsex);
//		if(listsNews != null && listsNews.size() > 0){
//			News news = listsNews.get(0);
//			ret.setAdTitle(news.getTitle());
//			ret.setAdUrl(news.getUrl());
//			ret.setAdImage(news.getImg());
//		}
		//更新大神笔记的未读
		Unread read = new Unread();
		read.setUserid(mobileInfo.getUserid());
		read.setModule1(0);
		System.out.println(read.getUserid());
		readService.updateByPrimaryKeySelective(read);
		//现获取科目列表
		NoteExample nex = new NoteExample();
		PaginationSupport.setContext(page, limit, null);
		List<SubjectDetail> details = null;
		if(StringUtils.isBlank(chapterId)){
			SubjectDetailExample sdex = new SubjectDetailExample();
			sdex.createCriteria().andSubIdEqualTo(type+"").andHasBooksEqualTo(1);
			sdex.setOrderByClause("top DESC,id DESC");
			details = subDetailService.selectByExample(sdex);
		}else{
			SubjectDetail detail = subDetailService.selectByPrimaryKey(chapterId);
			details = new ArrayList<SubjectDetail>();
			details.add(detail);
		}
		for(SubjectDetail detail : details){
			MNoteBookDetail.Builder detailRet = MNoteBookDetail.newBuilder();
			detailRet.setTitle(detail.getName());
			nex.clear();
			nex.createCriteria().andSubIdEqualTo(detail.getId());
			nex.setOrderByClause("top DESC");
			List<Note> lists = noteService.selectByExample(nex);
			nex.setOrderByClause("modify_time DESC");
			for(Note note : lists){
				detailRet.addBooks(fillNoteData(mobileInfo,note, authortService.selectByPrimaryKey(note.getAuthor()), 0));
			}
			ret.addNotes(detailRet);
		}
		//获得下一页的URL
		Map<String,String> params = ApiUtil.createParamMaps();
		if(StringUtils.isNotBlank(chapterId)){
			params.put("chapterId", chapterId);
		}
		ret.setNext(ApiUtil.getNextPageUrl("MNoteBookList", mobileInfo, PaginationSupport.getContext().getTotalCount() ,page, limit,params));
		return ret;
	}
	
	/**
	 * 获取某一个章节下的所有笔记
	 * @param mobileInfo
	 * @param chapterId
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MChapterList", isLogin = true, isPage = false) 
	public MSubject.Builder chapterList(MobileInfo mobileInfo,Integer type) throws Exception {
		checkParam(type, "type");
		MSubject.Builder ret = MSubject.newBuilder();
		SubjectDetailExample sdex = new SubjectDetailExample();
		sdex.createCriteria().andSubIdEqualTo(type+"").andHasBooksEqualTo(1);
		sdex.setOrderByClause("top DESC,id DESC");
		List<SubjectDetail> details = subDetailService.selectByExample(sdex);
		for(SubjectDetail item : details){
			MSubjectDetail.Builder detail = MSubjectDetail.newBuilder();
			detail.setId(item.getId());
			detail.setTitle(item.getName());
			ret.addDetails(detail);
		}
		return ret;
	}
	
	
	/**
	 * 获取笔记详情
	 * @param mobileInfo
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MNoteBookDetail", isLogin = true, isPage = false) 
	public MNoteBook.Builder noteBookDetail(MobileInfo mobileInfo,String id) throws Exception {
		checkParam(id, "id");
		Note note = noteService.selectByPrimaryKey(id);
		//增加浏览次数
		note.setBrowseCount(note.getBrowseCount()+1);
		noteService.updateByPrimaryKeySelective(note);
		//获取笔记详情
		MNoteBook.Builder ret = fillNoteData(mobileInfo,note, authortService.selectByPrimaryKey(note.getAuthor()), 1);
		return ret;
	}
	
	/**
	 * 获取某一个笔记本的全部笔记图片
	 * @param mobileInfo
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MNoteBookDetailList", isLogin = true, isPage = false)
	public MNoteDetailList.Builder noteDetailList(MobileInfo mobileInfo,String id)throws Exception{
		checkParam(id, "id");
		MNoteDetailList.Builder ret = MNoteDetailList.newBuilder();
		List<NoteUserEx> lists = getDetailListForUser(mobileInfo.getUserid(), id, 0);
		for(NoteUserEx nuex : lists){
			ret.addList(fillNoteDetail(nuex));
		}
		return ret;
	}
	
	
	/**
	 * 对笔记打重点标签
	 * @param mobileInfo
	 * @param id
	 * @param highlight
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MNoteDetailHighlight", isLogin = false, isPage = false)
	public MRet.Builder highlight(MobileInfo mobileInfo,String id,Integer type)throws Exception {
		checkParam(id, "id");
		if(type == null){
			type = 1;
		}
		NoteUserExample nuex = new NoteUserExample();
		nuex.createCriteria().andPidEqualTo(id).andUseridEqualTo(mobileInfo.getUserid());
		NoteUser nu = new NoteUser();
		nu.setPid(id);
		nu.setUserid(mobileInfo.getUserid());
		nu.setIsHighlight(type);
		nu.setModifyTime(new Date());
		noteUserService.updateByExampleSelective(nu, nuex);
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(1);
		return ret;
	}
	
	/**
	 * 对笔记添加备注
	 * @param mobileInfo
	 * @param id
	 * @param remark
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MNoteDetailRemark", isLogin = false, isPage = false)
	public MRet.Builder remark(MobileInfo mobileInfo,String id,String remark)throws Exception {
		if(StringUtils.isBlank(remark)){
			remark = "";
		}
		NoteUserExample nuex = new NoteUserExample();
		nuex.createCriteria().andPidEqualTo(id).andUseridEqualTo(mobileInfo.getUserid());
		NoteUser nu = new NoteUser();
		nu.setPid(id);
		nu.setUserid(mobileInfo.getUserid());
		nu.setRemark(remark);
		nu.setModifyTime(new Date());
		noteUserService.updateByExampleSelective(nu, nuex);
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(1);
		return ret;
	}
	
	
	
	/**
	 * 获取笔记本的评论列表
	 * @param mobileInfo
	 * @param id
	 * @param page
	 * @param limit
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MNoteComments", isLogin = false, isPage = false)
	public MNoteComments.Builder comments(MobileInfo mobileInfo,String id,Integer page, Integer limit)throws Exception {
		checkParam(id, "id");
		if(page == null)
			page = 1;
		if(limit == null)
			limit = 10;
		MNoteComments.Builder ret = MNoteComments.newBuilder();
		NoteCommentExample ncex = new NoteCommentExample();
		ncex.createCriteria().andNoteidEqualTo(id);
		ncex.setOrderByClause("create_time DESC");
		PaginationSupport.setContext(page, limit, null);
		List<NoteComment> comments = noteCommentService.selectByExample(ncex);
		for(NoteComment comment : comments){
			ret.addComments(fillNoteComment(comment));
		}
		//获得下一页的URL
		Map<String,String> params = ApiUtil.createParamMaps();
		if(StringUtils.isNotBlank(id)){
			params.put("id", id);
		}
		ret.setNext(ApiUtil.getNextPageUrl("MNoteComments", mobileInfo, PaginationSupport.getContext().getTotalCount() ,page, limit,params));
		return ret;
	}
	
	/**
	 * 笔记本点赞 type为1表示点赞，为0表示取消点赞
	 * @param mobileInfo
	 * @param id
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MNotePraise", isLogin = true)
	public MRet.Builder praise(MobileInfo mobileInfo,String id,Integer type) throws Exception {
		MRet.Builder ret = MRet.newBuilder();
		checkParam(id, "id");
		if(type == null){
			type = 1;
		}
		//首先看是否有过点赞记录了
		NotePraiseExample npex = new NotePraiseExample();
		npex.createCriteria().andNoteIdEqualTo(id).andUseridEqualTo(mobileInfo.getUserid());
		List<NotePraise> lists = notePraiseService.selectByExample(npex);
		if(lists != null && lists.size() > 0){
			NotePraise praise = lists.get(0);
			praise.setType(type);
			praise.setModidyTime(new Date());
			notePraiseService.updateByPrimaryKeySelective(praise);
		}else{
			NotePraise praise = new NotePraise();
			praise.setCreateTime(new Date());
			praise.setUserid(mobileInfo.getUserid());
			praise.setType(1);
			praise.setNoteId(id);
			praise.setUserImgPath("");
			User user = userService.selectByPrimaryKey(mobileInfo.getUserid());
			if(user != null){
				praise.setUserImg(StringUtil.emptyIfBlank(user.getHeadimg()));
			}
			notePraiseService.insert(praise);
		}
		Note note = noteService.selectByPrimaryKey(id);
		//更新点赞数目
		if(type == 1){
			note.setPraiseCount(note.getPraiseCount() + 1);
		}else{
			note.setPraiseCount(note.getPraiseCount() - 1);
		}
		noteService.updateByPrimaryKeySelective(note);
		ret.setCode(1);
		return ret;
	}
	
	/**
	 * 收藏一本笔记
	 * @param mobileInfo
	 * @param id
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MNoteCollect", isLogin = true)
	public MRet.Builder collect(MobileInfo mobileInfo,String id,Integer type) throws Exception {
		if(type == null){
			type = 1;
		}
		NoteCollectKey key = new NoteCollectKey();
		key.setPid(id);
		key.setUserid(mobileInfo.getUserid());
		NoteCollect collect = noteCollectService.selectByPrimaryKey(key);
		if(collect == null){
			collect = new NoteCollect();
			collect.setPid(id);
			collect.setUserid(mobileInfo.getUserid());
			collect.setType(1);
			collect.setCreateTime(new Date());
			collect.setModifyTime(collect.getCreateTime());
			noteCollectService.insert(collect);
		}else{
			collect.setType(type);
			collect.setModifyTime(new Date());
			noteCollectService.updateByPrimaryKey(collect);
		}
		//
		//更新点赞数目
		Note note = noteService.selectByPrimaryKey(id);
		if(type == 1){
			note.setPraiseCount(note.getPraiseCount() + 1);
		}else{
			note.setPraiseCount(note.getPraiseCount() - 1);
		}
		noteService.updateByPrimaryKeySelective(note);
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(1);
		return ret;
	}
	
	/**
	 * 笔记分享之后的回调请求
	 * @param mobileInfo
	 * @param id
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MNoteShare", isLogin = true)
	public MRet.Builder share(MobileInfo mobileInfo,String id) throws Exception {
		NoteLockKey key = new NoteLockKey();
		key.setPid(id);
		key.setUserid(mobileInfo.getUserid());
		if(noteLockService.selectByPrimaryKey(key)==null){
			NoteLock lock = new NoteLock();
			lock.setPid(key.getPid());
			lock.setUserid(key.getUserid());
			lock.setCreateTime(new Date());
			noteLockService.insert(lock);
		}		
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(1);
		return ret;
	}
	
	
	/**
	 * 评论笔记本
	 * @param mobileInfo
	 * @param id
	 * @param content
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MNoteComment", isLogin = true)
	public MRet.Builder comment(MobileInfo mobileInfo,String id,String content) throws Exception {
		MRet.Builder ret = MRet.newBuilder();
		checkParam(id, "id");
		checkParam(content, content);
		NoteComment comment = new NoteComment();
		comment.setContent(content);
		comment.setCreateTime(new Date());
		comment.setIsShow(1);
		comment.setNoteid(id);
		comment.setUserid(mobileInfo.getUserid());
		User user = userService.selectByPrimaryKey(mobileInfo.getUserid());
		if(user != null){
			comment.setUserimg(StringUtil.emptyIfBlank(user.getHeadimg()));
			comment.setUsername(StringUtil.emptyIfBlank(user.getNickname()));
		}
		noteCommentService.insert(comment);
		//更新评论数目
		Note note = noteService.selectByPrimaryKey(id);
		note.setCommentCount(note.getCommentCount() + 1);
		noteService.updateByPrimaryKeySelective(note);
		ret.setCode(1);
		return ret;
	}
	
	@MobileMethod(methodno = "MMyCollect", isLogin = true,isPage = false)
	public MMyCollectList.Builder mycollects(MobileInfo mobileInfo,Integer type,Integer page,Integer limit) throws Exception {
//		checkParam(type, "type");
		MMyCollectList.Builder ret = MMyCollectList.newBuilder();
		//先获取新闻收藏
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userid", mobileInfo.getUserid());
//		PaginationSupport.setContext(page, limit, null);
		List<NewsEx> newsLists = newsService.selectMyCollectNews(map);
		for(NewsEx news : newsLists){
			MMyCollect.Builder collect = MMyCollect.newBuilder();
			if(!news.getUrl().startsWith("http")){
				collect.setUrl(String.format("%s%s?newsid=%s", ConstantResources.OSS_OUT_WEB_URL,news.getUrl(),news.getId()));
			}else{
				collect.setUrl(String.format("%s?newsid=%s", news.getUrl(),news.getId()));
			}
			collect.setId(news.getId());
			collect.setTitle(news.getTitle());
			collect.setDescription(news.getDescription());
			collect.setType(1);
			collect.setCount(news.getBrowseCount());
			collect.setImg(news.getImg());
			collect.setDate(DateUtil.dateToStr(news.getCreateTime(), "yyyy-MM-dd"));
			ret.addCollects(collect);
		}
//		//再组装笔记收藏
		List<Note> notesLists = noteService.selectMyCollectNotes(map);
		for(Note note : notesLists){
			MMyCollect.Builder collect = MMyCollect.newBuilder();
			collect.setId(note.getId());
			collect.setTitle(note.getDescription());
			collect.setDescription(note.getDescription());
			collect.setType(2);
			collect.setCount(note.getPraiseCount());
			collect.setImg(note.getCover());
			collect.setDate(DateUtil.dateToStr(note.getCreateTime(), "yyyy-MM-dd"));
			ret.addCollects(collect);
		}
//		//最后组装名师收藏
		List<EssenceAuthor> Lists = essenceAuthorService.selectMyCollectAuthors(map);
		for(EssenceAuthor author : Lists){
			MMyCollect.Builder collect = MMyCollect.newBuilder();
			collect.setId(author.getId());
			collect.setTitle(author.getName());
			collect.setDescription(author.getDescription());
			collect.setType(3);
			collect.setImg(author.getHeadimg());
			collect.setDate(DateUtil.dateToStr(author.getCreateTime(), "yyyy-MM-dd"));
			collect.setCount(author.getCollectCount());
			ret.addCollects(collect);
		}

//		//获得下一页的URL
//		Map<String, String> params = ApiUtil.createParamMaps();
//		params.put("type", type+"");
//		ret.setNext(ApiUtil.getNextPageUrl("MMyCollect", mobileInfo, PaginationSupport.getContext().getTotalCount() ,page, limit,params));
		return ret;
	}
	
	/**
	 * 获取用户的大神笔记列表，如果不存在则先为用户填充数据再后再返回
	 * @param userid
	 * @param noteId
	 * @param ret
	 * @param type  为1时表示需要限制分页和数目
	 */
	public List<NoteUserEx> getDetailListForUser(String userid,String noteId,int type){
		Map<String,String> map = new HashMap<String, String>();
		map.put("userid", userid);
		map.put("noteId", noteId);
		NoteUserExample nuex = new NoteUserExample();
		nuex.createCriteria().andNoteIdEqualTo(noteId).andUseridEqualTo(userid);
		if(type == 1){
			PaginationSupport.setContext(1, 8, null);
		}
		List<NoteUserEx> lists = noteUserService.selectNoteUserEx(map);
		if(lists == null || lists.size() == 0){
			initNoteUser(userid, noteId);
			lists = noteUserService.selectNoteUserEx(map);
		}
		return lists;
	}
	
	/**
	 * 为用户初始化大神笔记的列表
	 * @param userid
	 * @param noteId
	 */
	public void initNoteUser(String userid,String noteId){
		NoteDetailExample ndex = new NoteDetailExample();
		ndex.setOrderByClause("orders ASC");
		ndex.createCriteria().andPidEqualTo(noteId);
		List<NoteDetail> lists = noteDetailService.selectByExample(ndex);
		Date now = new Date();
		for(NoteDetail detail : lists){
			NoteUser nu = new NoteUser();
			nu.setPid(detail.getId());
			nu.setNoteId(noteId);
			nu.setUserid(userid);
			nu.setIsHighlight(detail.getIsAd());
			nu.setRemark(detail.getRemark());
			nu.setRotation(1);
			nu.setCreateTime(now);
			nu.setTop(detail.getOrders());
			noteUserService.insert(nu);
		}
	}
	
	
	/**
	 * 检查是否已经解锁，是的话返回0，否的话返回1
	 * @param noteId
	 * @param userid
	 * @return
	 */
	public int checkUnlockde(String noteId, String userid){
		int result = 1;
		NoteLockKey key = new NoteLockKey();
		key.setPid(noteId);
		key.setUserid(userid);
		//说明该笔记已经对该用户解锁了
		if(noteLockService.selectByPrimaryKey(key) != null){
			result = 0;
		}
		return result;
	}
	
	/**
	 * 填充笔记本数据  type为0的时候表示只需要获取图片，id等几个简单信息；type为1的时候表示获取所有的信息
	 * @param mobileInfo
	 * @param note
	 * @param author
	 * @param type
	 * @return
	 */
	public MNoteBook.Builder fillNoteData(MobileInfo mobileInfo,Note note,NoteAuthor author,int type){
		MNoteBook.Builder ret = MNoteBook.newBuilder();
		ret.setId(note.getId());
		ret.setSubId(note.getSubId());
		ret.setTitle(note.getTitle());
		ret.setDesc(note.getDescription());
		ret.setSubNumber(note.getSubNum());
		ret.setCover(note.getCover());
		//需要判断下这个用户有没有解锁
		if(note.getIsLocked() == 1){
			ret.setIsLocked(checkUnlockde(note.getId(), mobileInfo.getUserid()));
		}else{
			ret.setIsLocked(0);
		}
		ret.setBrowseCount(note.getBrowseCount());
		ret.setAuthorId(author.getId());
		ret.setAuthorName(author.getName());
		ret.setAuthorImg(author.getImg());
		ret.setAuthorDesc(author.getDescription());
		ret.setAuthorSex(author.getSex());
		ret.setCollectCount(note.getPraiseCount());
		//分享的url
		ret.setShareUrl(String.format("%s?noteId=%s&t=%d",ConstantResources.NOTEURL,note.getId(),System.currentTimeMillis()));
		//type==1时是获取详情，需要额外取一些信息，包括：点赞数目，点赞的头像列表，评论数目，并取前两条评论
		if(type == 1){
			//首先拿科目信息
			SubjectDetail subDetail = subDetailService.selectByPrimaryKey(note.getSubId());
			ret.setSubName(subDetail.getSubName());
			ret.setSubDetailName(subDetail.getName());
			//再去拿点赞数目
//			NotePraiseExample npex = new NotePraiseExample();
//			npex.createCriteria().andNoteIdEqualTo(note.getId()).andTypeEqualTo(1);
//			if(ret.getPraiseCount() > 0){
//				npex.setOrderByClause("create_time DESC");
//				PaginationSupport.setContext(1, 20, null);
//				List<NotePraise> praises = notePraiseService.selectByExample(npex);
//				for(NotePraise praise : praises){
//					ret.addPraiseImgs(praise.getUserImg());
//				}
//			}
			//是否点过赞
//			npex.clear();
//			npex.createCriteria().andNoteIdEqualTo(note.getId()).andTypeEqualTo(1).andUseridEqualTo(mobileInfo.getUserid());
//			List<NotePraise> praises = notePraiseService.selectByExample(npex);
//			if(praises == null || praises.size() == 0){
//				ret.setHasPraised(0);
//			}else{
//				ret.setHasPraised(1);
//			}
			//评论数目
			ret.setCommentCount(note.getCommentCount());
			NoteCommentExample ncex = new NoteCommentExample();
			ncex.createCriteria().andNoteidEqualTo(note.getId());
			if(ret.getCommentCount() > 0){
				ncex.setOrderByClause("create_time DESC");
				List<NoteComment> comments = noteCommentService.selectByExample(ncex);
				PaginationSupport.setContext(1, 5, null);
				for(NoteComment comment : comments){
					ret.addComments(fillNoteComment(comment));
				}
			}
			//是否收藏
			NoteCollectExample nocex = new NoteCollectExample();
			nocex.createCriteria().andPidEqualTo(note.getId()).andUseridEqualTo(mobileInfo.getUserid()).andTypeEqualTo(1);
			ret.setHasCollect(noteCollectService.countByExample(nocex));
			//大神信息
			if(StringUtils.isNotBlank(author.getVideo())){
				ret.setAuthorType(1);
				ret.setAuthorVideo(author.getVideo());
				ret.setVideoCover(author.getVideoCover());
			}else{
				ret.setAuthorImgList(author.getOtherImgs());
//				List<NoteUserEx> lists = getDetailListForUser(mobileInfo.getUserid(), note.getId(), 1);
//				for(NoteUserEx nuex : lists){
//					ret.addList(fillNoteDetail(nuex));
//				}
			}
			ret.setWantToSay(note.getWantToSay());
			ret.setSuitable(note.getSuitable());
			ret.setExperience(note.getExperience());
		}
		return ret;
	}
	
	/**
	 * 填充笔记详情数据
	 * @param u
	 * @return
	 */
	public MQuestion.Builder fillNoteDetail(NoteUserEx u){
		MQuestion.Builder detailRet = MQuestion.newBuilder();
		detailRet.setId(u.getPid());
		detailRet.setImg(u.getImg());
		detailRet.setIsHighlight(u.getIsHighlight());
		detailRet.setRemark(u.getRemark());
		detailRet.setUserid(u.getUserid());
		detailRet.setSubject(u.getSubName());
		detailRet.setStatus(1);
		return detailRet;
	}
	
	/**
	 * 填充笔记评论数据
	 * @param comment
	 * @return
	 */
	public MNoteComment.Builder fillNoteComment(NoteComment comment){
		MNoteComment.Builder ret = MNoteComment.newBuilder();
		ret.setId(comment.getId());
		ret.setContent(comment.getContent());
		ret.setHeadimg(comment.getUserimg());
		ret.setUserid(comment.getUserid());
		ret.setNickname(comment.getUsername());
		ret.setPid(comment.getNoteid());
		ret.setTime(DateUtil.getLnow(comment.getCreateTime()));
		return ret;
	}
	
	
	
	/**
	 * 获取所有的科目列表  目前废弃！！！！
	 * @param mobileInfo
	 * @return
	 * @throws Exception
	 */
	@MobileMethod(methodno = "MSubjectsAll", isLogin = true, isPage = false) 
	public MSubjectList.Builder subjectsAll(MobileInfo mobileInfo) throws Exception {
		MSubjectList.Builder ret = MSubjectList.newBuilder();
		SubjectExample sjex = new SubjectExample();
		sjex.setOrderByClause("top ASC");
		List<Subject> subjects =  subService.selectByExample(sjex);
		//拿一条广告
		NewsExample nex = new NewsExample();
		nex.createCriteria().andTypeEqualTo(2).andIsShowEqualTo(1);
		nex.setOrderByClause("top DESC");
		List<News> lists = newsService.selectByExample(nex);
		if(lists != null && lists.size() > 0){
			News news = lists.get(0);
			ret.setAdTitle(news.getTitle());
			ret.setAdUrl(news.getUrl());
		}
		//再去组装科目
		SubjectDetailExample sjdx = new SubjectDetailExample();
		for(Subject subject : subjects){
			MSubject.Builder subRet = MSubject.newBuilder();
			subRet.setId(subject.getId());
			subRet.setTitle(subject.getName());
			sjdx.clear();
			sjdx.createCriteria().andSubIdEqualTo(subject.getId());
			sjex.setOrderByClause("top ASC");
			List<SubjectDetail> details = subDetailService.selectByExample(sjdx);
			for(SubjectDetail detail : details){
				MSubjectDetail.Builder detailRet = MSubjectDetail.newBuilder();
				detailRet.setId(detail.getId());
				detailRet.setTitle(detail.getName());
				subRet.addDetails(detailRet);
			}
			ret.addSubjects(subRet);
		}
		return ret;
	}
	
	
	
}
